package cn.ekuma.epos.datalogic.define.dao.erp.subject;

import com.openbravo.bean.Product;
import com.openbravo.bean.erp.subject.Subject;
import com.openbravo.bean.erp.subject.SubjectProduct;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.DataWrite;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.KeyBuilder;
import com.openbravo.data.loader.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;

import cn.ekuma.data.dao.ModifiedLogDAO;
import cn.ekuma.data.dao.bean.I_ModifiedLogBean;
import cn.ekuma.epos.datalogic.define.dao.ProductDAO;
import cn.ekuma.epos.db.table.erp.subject.I_SubjectProduct;

public class SubjectProductDAO extends ModifiedLogDAO<SubjectProduct>{

	ProductDAO productDAO;
	public SubjectProductDAO(I_Session s,ProductDAO productDAO) {
		super(s);
		this.productDAO=productDAO;
	}

	public SubjectProductDAO(I_Session s) {
		super(s);
	}

	@Override
	public SubjectProduct readValues(DataRead dr,SubjectProduct obj) throws BasicException {
		if(obj==null)
			obj=new SubjectProduct();
		obj.setId(dr.getString(1));
		obj.setSubjectId(dr.getString(2));
		obj.setProductID(dr.getString(3));
		if(productDAO!=null)
		   obj.setIncludeProduct(productDAO.find(KeyBuilder.getKey(dr.getString(3))));
		obj.setAttrInsValue(dr.getString(4));
		obj.setUnit(dr.getDouble(5));
		obj.setLastModified(dr.getTimestamp(6));
		return obj;
	}

	@Override
	public TableDefinition getTable() {
		return new TableDefinition(s,
                "SUBJECT_PRODUCT",
                new Field[]{
                   new Field("ID",Datas.STRING, Formats.STRING),
                   new Field(I_SubjectProduct.SUBJECT,Datas.STRING, Formats.STRING,Subject.class),
                   new Field(I_SubjectProduct.PRODUCT,Datas.STRING, Formats.STRING,Product.class),
                   new Field(I_SubjectProduct.ATTRIBUTESETINSTANCE_ID,Datas.STRING, Formats.STRING),
                   new Field(I_SubjectProduct.UNITS,Datas.DOUBLE, Formats.DOUBLE),
                   new Field(I_ModifiedLogBean.LASTMODIFIED,Datas.TIMESTAMP, Formats.TIMESTAMP)
                 },
                new int[]{0}
                );
	}

	@Override
	public void writeInsertValues(DataWrite dp, SubjectProduct obj)
			throws BasicException {
		dp.setString(1,obj.getId());
		dp.setString(2,obj.getSubjectId());
		dp.setString(3,obj.getProductID());
		dp.setString(4,obj.getAttrInsValue());
		dp.setDouble(5,obj.getUnit());
		dp.setTimestamp(6,obj.getLastModified());
		
	}

	@Override
	public Class getSuportClass() {
		return SubjectProduct.class;
	}

}
